<template lang="">
    <div>
        <van-nav-bar  title="频道管理">
          <template #right>
               <van-icon name="cross" size="0.47533334rem" color="#fff" @click="backHome"/>
          </template>
        </van-nav-bar>
        <div class="edit_title clearfix">
             <span class="my_channel">我的频道<span class="click_channel">点击{{isEdit?'删除':'进入'}}频道</span></span><span class="edit_mychannel" @click="isEdit=!isEdit">{{isEdit?'完成':'编辑'}}</span>
        </div>
        <div class="channel_content">
                <van-row type="flex">
                <van-col  v-for="obj in userChannels" :key="obj.id"  @click="removeChannel(obj)" >{{obj.name}}<van-icon name="cross" size="0.35533334rem" color="#ccc" class="cross_channel" v-show="isEdit"/></van-col>
                </van-row>
        </div>
         <div class="edit_title clearfix">
             <span class="my_channel">点击添加更多的频道：</span>
        </div>
         <div class="channel_content">
                <van-row type="flex">
                <van-col v-for="obj in otherChannels" :key="obj.id" @click="addChannel(obj)" >{{obj.name}}</van-col>
                </van-row>
        </div>
    </div>
</template>
<script>
import { mapGetters, mapState } from 'vuex'
export default {
  name: 'EditChannel',
  data () {
    return {
      isEdit: false
    }
  },
  computed: {
    ...mapState('home', { userChannels: 'userChannels' }),
    ...mapGetters('home', { otherChannels: 'otherChannels' })
  },
  methods: {
    addChannel (obj) {
      if (this.isEdit) {
        this.$store.dispatch('home/addChannels', obj)
      }
    },
    backHome () {
      this.$emit('backHome')
      if (this.isEdit) {
        this.isEdit = false
      }
    },
    // 删除频道
    removeChannel (obj) {
      console.log('enter1')
      if (this.isEdit) {
        if (obj.id !== 0) {
          this.$store.dispatch('home/removeChannels', obj)
        }
      } else {
        console.log('enter2')
        this.$emit('backHome')
        // 触发input函数
        this.$emit('input', obj.id)
      }
    }
    // 进入相应的频道
  }
}
</script>
<style lang="less" scoped>
.edit_title{
    padding:10px 10px 5px 5px;
.my_channel{
    float: left;
    font-size: 16px;
    .click_channel{
       font-size: 14px;
       color: #ccc;
       padding-left: 5px;
    }
}
.edit_mychannel{
    float: right;
    font-size: 16px;
}
}
.clearfix::after,.clearfix::before
{
    content:'';
    display: table;
    clear: both;
}
.channel_content{
    margin-top: 15px;
 /deep/ .van-col{
       position: relative;
       background-color:rgba(0, 0, 0, 0.03);
       height: 40px;
       width: 88px;
       line-height: 40px;
       font-size: 16px;
       text-align: center;
       border-radius: 3px;
       margin-bottom: 10px;
       margin-right: 5px;
       .cross_channel{
        position: absolute;
        top:0px;
        right: 1px;
       }
    }
  /deep/ .van-row
  {
    margin-bottom: 10px;
  }
}
</style>
